
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Ajax请求 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="MOCK.html" />
    
    
    <link rel="prev" href="ROUTE.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="START.html">
            
                <a href="START.html">
            
                    
                    快速开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="MENU.html">
            
                <a href="MENU.html">
            
                    
                    菜单配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="PAGE.html">
            
                <a href="PAGE.html">
            
                    
                    页面开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="ROUTE.html">
            
                <a href="ROUTE.html">
            
                    
                    系统路由
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6" data-path="AJAX.html">
            
                <a href="AJAX.html">
            
                    
                    Ajax请求
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="MOCK.html">
            
                <a href="MOCK.html">
            
                    
                    Mock模拟数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="STYLE.html">
            
                <a href="STYLE.html">
            
                    
                    样式&主题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="LAYOUT.html">
            
                <a href="LAYOUT.html">
            
                    
                    导航布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="MODEL.html">
            
                <a href="MODEL.html">
            
                    
                    Model(Redux)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="I18N.html">
            
                <a href="I18N.html">
            
                    
                    国际化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="PERMISSION.html">
            
                <a href="PERMISSION.html">
            
                    
                    权限控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="NGINX.html">
            
                <a href="NGINX.html">
            
                    
                    Nginx配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="PROXY.html">
            
                <a href="PROXY.html">
            
                    
                    开发代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="OTHER.html">
            
                <a href="OTHER.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Ajax请求</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="ajax-&#x8BF7;&#x6C42;">Ajax &#x8BF7;&#x6C42;</h1>
<p>&#x7CFB;&#x7EDF;&#x7684;ajax&#x8BF7;&#x6C42;&#x57FA;&#x4E8E;<a href="https://github.com/axios/axios" target="_blank">axios</a>&#x5C01;&#x88C5;&#x3002;</p>
<h2 id="&#x65B9;&#x6CD5;">&#x65B9;&#x6CD5;</h2>
<p>&#x57FA;&#x4E8E;restful&#x89C4;&#x8303;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;5&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<ul>
<li>get &#x83B7;&#x53D6;&#x670D;&#x52A1;&#x7AEF;&#x6570;&#x636E;&#xFF0C;&#x53C2;&#x6570;&#x62FC;&#x63A5;&#x5728;url&#x4E0A;&#xFF0C;&#x4EE5; query string&#x65B9;&#x5F0F;&#x53D1;&#x9001;&#x7ED9;&#x540E;&#x7AEF;</li>
<li>post &#x65B0;&#x589E;&#x6570;&#x636E;&#xFF0C;&#x53C2;&#x6570;&#x4EE5;body&#x5F62;&#x5F0F;&#x53D1;&#x9001;&#x7ED9;&#x540E;&#x7AEF;</li>
<li>put &#x4FEE;&#x6539;&#x6570;&#x636E;&#xFF0C;&#x53C2;&#x6570;&#x4EE5;body&#x5F62;&#x5F0F;&#x53D1;&#x9001;&#x7ED9;&#x540E;&#x7AEF;</li>
<li>del &#x5220;&#x9664;&#x6570;&#x636E;&#xFF0C;&#x53C2;&#x6570;&#x62FC;&#x63A5;&#x5728;url&#x4E0A;&#xFF0C;&#x4EE5;params&#x65B9;&#x5F0F;&#x53D1;&#x9001;&#x7ED9;&#x540E;&#x7AEF;</li>
<li>patch &#x4FEE;&#x6539;&#x90E8;&#x5206;&#x6570;&#x636E;&#xFF0C;&#x53C2;&#x6570;&#x4EE5;body&#x5F62;&#x5F0F;&#x53D1;&#x9001;&#x7ED9;&#x540E;&#x7AEF;</li>
</ul>
<h2 id="&#x8C03;&#x7528;&#x65B9;&#x5F0F;">&#x8C03;&#x7528;&#x65B9;&#x5F0F;</h2>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x8BA9;React&#x7EC4;&#x4EF6;&#x62FF;&#x5230;ajax&#x5BF9;&#x8C61;</p>
<ul>
<li><p>config&#x88C5;&#x9970;&#x5668;ajax&#x5C5E;&#x6027;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">import</span> React, {Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
  <span class="hljs-keyword">import</span> config <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/commons/config-hoc&apos;</span>;

  @config({
      ajax: <span class="hljs-literal">true</span>,
      ...
  })
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomePage</span> <span class="hljs-title">extend</span> <span class="hljs-title">Component</span> </span>{
      componentDidMount() {
          <span class="hljs-keyword">this</span>.props.ajax
              .get(...)
              .then(...)
      }
      ...
  }
</code></pre>
</li>
<li><p>ajax&#x88C5;&#x9970;&#x5668;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">import</span> React, {Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
  <span class="hljs-keyword">import</span> {ajaxHoc} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/commpons/ajax&apos;</span>;

  @ajaxHoc()
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomePage</span> <span class="hljs-title">extend</span> <span class="hljs-title">Component</span> </span>{
      componentDidMount() {
          <span class="hljs-keyword">this</span>.props.ajax
              .get(...)
              .then(...)
      }
      ...
  }
</code></pre>
</li>
<li><p>&#x76F4;&#x63A5;&#x5F15;&#x5165;ajax&#x5BF9;&#x8C61;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">import</span> React, {Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
  <span class="hljs-keyword">import</span> {sxAjax} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/commpons/ajax&apos;</span>;

  @ajaxHoc()
  <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomePage</span> <span class="hljs-title">extend</span> <span class="hljs-title">Component</span> </span>{
      componentDidMount() {
          sxAjax.post(...).then(...);

          <span class="hljs-comment">// &#x7EC4;&#x4EF6;&#x5378;&#x8F7D;&#x6216;&#x8005;&#x5176;&#x4ED6;&#x4EC0;&#x4E48;&#x60C5;&#x51B5;&#xFF0C;&#x9700;&#x8981;&#x6253;&#x7B97;ajax&#x8BF7;&#x6C42;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;</span>
          <span class="hljs-keyword">const</span> ajax = sxAjax.get(...);
          ajax.then(...).finally(...);
          ajax.cancel();
      }
      ...
  }
</code></pre>
<p>&#x6CE8;&#xFF1A;config&#x3001;ajaxHoc&#x65B9;&#x5F0F;&#x505A;&#x4E86;&#x5C01;&#x88C5;&#xFF0C;&#x9875;&#x9762;&#x88AB;&#x5378;&#x8F7D;&#x4E4B;&#x540E;&#x4F1A;<strong>&#x81EA;&#x52A8;&#x6253;&#x65AD;</strong>&#x672A;&#x5B8C;&#x6210;&#x7684;&#x8BF7;&#x6C42;</p>
</li>
</ul>
<h2 id="&#x53C2;&#x6570;">&#x53C2;&#x6570;</h2>
<p>&#x6240;&#x6709;&#x7684;ajax&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x7EDF;&#x4E00;&#xFF0C;&#x90FD;&#x80FD;&#x591F;&#x63A5;&#x53D7;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>url</td>
<td>&#x8BF7;&#x6C42;&#x5730;&#x5740;</td>
</tr>
<tr>
<td>params</td>
<td>&#x8BF7;&#x6C42;&#x4F20;&#x9012;&#x7ED9;&#x540E;&#x7AEF;&#x7684;&#x53C2;&#x6570;</td>
</tr>
<tr>
<td>options</td>
<td>&#x8BF7;&#x6C42;&#x914D;&#x7F6E;&#xFF0C;&#x5373;axios&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x6269;&#x5C55;&#x4E86;&#x4E09;&#x4E2A;&#x4E2A;&#xFF1A;successTip errorTip&#xFF0C;&#x6210;&#x529F;&#x6216;&#x5931;&#x8D25;&#x63D0;&#x793A;&#xFF1B;noEmpty&#x8FC7;&#x6EE4;&#x6389; &apos;&apos;&#x3001;null&#x3001;undefined&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4E0D;&#x63D0;&#x4EA4;&#x7ED9;&#x540E;&#x7AEF;</td>
</tr>
</tbody>
</table>
<p>&#x6CE8;&#xFF1A;&#x5168;&#x5C40;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x5728;src/commons/ajax.js&#x4E2D;&#x8FDB;&#x884C;&#x914D;&#x7F6E;&#xFF0C;&#x9ED8;&#x8BA4;baseURL=&apos;/api&apos;&#x3001;timeout=1000 * 60&#x3002;</p>
<h2 id="&#x8BF7;&#x6C42;&#x7ED3;&#x679C;&#x63D0;&#x793A;">&#x8BF7;&#x6C42;&#x7ED3;&#x679C;&#x63D0;&#x793A;</h2>
<p>&#x7CFB;&#x7EDF;&#x5BF9;ajax&#x5931;&#x8D25;&#x505A;&#x4E86;&#x81EA;&#x52A8;&#x63D0;&#x793A;&#xFF0C;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x53EF;&#x901A;&#x8FC7;src/commons/handle-error.js&#x8FDB;&#x884C;&#x914D;&#x7F6E;&#xFF1B;</p>
<p>&#x6210;&#x529F;&#x63D0;&#x793A;&#x9ED8;&#x8BA4;&#x4E0D;&#x663E;&#x793A;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x6210;&#x529F;&#x63D0;&#x793A;&#xFF0C;&#x53EF;&#x4EE5;&#x914D;&#x7F6E;successTip&#x53C2;&#x6570;&#xFF0C;&#x6216;&#x8005;.then()&#x4E2D;&#x81EA;&#x884C;&#x5904;&#x7406;&#xFF1B;</p>
<p>&#x6210;&#x529F;&#x63D0;&#x793A;&#x5728;src/commons/handle-success.js&#x4E2D;&#x914D;&#x7F6E;&#xFF1B;</p>
<pre><code class="lang-js"><span class="hljs-keyword">this</span>.props.ajax.del(<span class="hljs-string">&apos;/user/1&apos;</span>, <span class="hljs-literal">null</span>, {successTip: <span class="hljs-string">&apos;&#x5220;&#x9664;&#x6210;&#x529F;&#xFF01;&apos;</span>, errorTip: <span class="hljs-string">&apos;&#x5220;&#x9664;&#x5931;&#x8D25;&#xFF01;&apos;</span>, noEmpty: <span class="hljs-literal">true</span>});
</code></pre>
<h2 id="loading&#x5904;&#x7406;">loading&#x5904;&#x7406;</h2>
<p>&#x7CFB;&#x7EDF;&#x6269;&#x5C55;&#x4E86;promise&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;finally&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x65E0;&#x8BBA;&#x6210;&#x529F;&#x8FD8;&#x662F;&#x5931;&#x8D25;&#xFF0C;&#x90FD;&#x8981;&#x8FDB;&#x884C;&#x7684;&#x5904;&#x7406;&#x3002;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x5173;&#x95ED;loading</p>
<pre><code class="lang-js"><span class="hljs-keyword">this</span>.setState({loading: <span class="hljs-literal">true</span>});
<span class="hljs-keyword">this</span>.props.ajax
    .get(<span class="hljs-string">&apos;/url&apos;</span>)
    .then(...)
    .finally(() =&gt; <span class="hljs-keyword">this</span>.setState({loading: <span class="hljs-literal">false</span>}));
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="ROUTE.html" class="navigation navigation-prev " aria-label="Previous page: 系统路由">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="MOCK.html" class="navigation navigation-next " aria-label="Next page: Mock模拟数据">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Ajax请求","level":"1.6","depth":1,"next":{"title":"Mock模拟数据","level":"1.7","depth":1,"path":"MOCK.md","ref":"MOCK.md","articles":[]},"previous":{"title":"系统路由","level":"1.5","depth":1,"path":"ROUTE.md","ref":"ROUTE.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"AJAX.md","mtime":"2019-02-15T02:06:02.030Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-02-15T08:21:50.487Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

